package com.ateam.biz.impl;

import java.util.List;

import com.ateam.biz.MissionsBiz;
import com.ateam.dao.MissionsDao;
import com.ateam.model.ChartModel;
import com.ateam.pojo.Missions;
import com.ateam.util.JudgeTools;
import com.ateam.util.PojoFactory;

/** 
* @author WuYuan
* 
*/
public class MissionsBizImpl implements MissionsBiz {
	private MissionsDao missionsDao;
	
	public MissionsDao getMissionsDao() {
		return missionsDao;
	}

	public void setMissionsDao(MissionsDao missionsDao) {
		this.missionsDao = missionsDao;
	}

	@Override
	public int addMission(Missions mission, int leaderid, int[] memberids, int groupid, int projectid) {
		try {
			mission.setIscompleted("0");//默认未完成
			mission.setOverdue("0");//默认未逾期
			mission.setProjectid(projectid);//添加默认任务时，默认项目id是1
			mission.setGroupid(groupid);//添加默认任务时，默认小组id是1
			System.out.println("addmission中的mission是"+mission.toString());
			missionsDao.addMission(mission);//执行这一步后，mission对象获得自增的id
			for(int i=0; i<=memberids.length-1;i++){
				System.out.println("准备插入数据user_mission的id是"+memberids[i]+"  对应的missionid是"+mission.getMissionid());
				missionsDao.addMissionmember(memberids[i], mission.getMissionid());
			}
			if(JudgeTools.judgeObjectInArray(leaderid, memberids)){
				missionsDao.deleteMissionmember(leaderid, mission.getMissionid());
				return missionsDao.addMissionleader(leaderid, mission.getMissionid());
			}else
				return missionsDao.addMissionleader(leaderid, mission.getMissionid());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<Missions> getMissionByUserid(int userid) {
		try {
			return missionsDao.getMissionsByUserid(userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updateMissionComplete(int userid, int missionid) {
		try {
			return missionsDao.updateMissionComplete(userid, missionid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public ChartModel getChartInfo(int companyid) {
		ChartModel chart = PojoFactory.getChart();
		try {
			chart.setMissioncompleted(missionsDao.countMissionsHaveDone(companyid));
			chart.setMissionnotcompleted(missionsDao.countMissionsHaventDone(companyid));
			return chart;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Missions> getMissionDoneByUserid(int userid) {
		try {
			return missionsDao.getMissionsDoneByUserid(userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Missions getMissionByMissionid(int missionid) {
		try {
			return missionsDao.getMissionByMissionid(missionid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
